// pages/home/home.js

//获取当前日期
import util from '../../utils/util';
const DATE = util.formatTime(new Date())

const app = getApp()
const db = wx.cloud.database().collection('ticketList');
const db2 = wx.cloud.database().collection('resultList')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        ticketList: [],
        resultList: [],
        user: null,
        DATE:DATE,
    },

    //获取抢号列表

    getTicketList(e) {
        db.orderBy('_id','desc').get({
            success: (res) => {
                console.log("当前可抢号列表：");
                console.log(res.data);
                this.setData({
                    ticketList: res.data
                })
                //获取用户选号名单
                this.getResultList()
            }
        })
    },
    //抢号
    getTicket(e) {
        //获取当前点击项数据

        var data = e.currentTarget.dataset.data
        console.log("当前点击项数据：");
        console.log(data)


        //判断抢号状态
        var d1 = Date.parse(DATE)
        var d2 = Date.parse(data.sTime)
        var d3 = Date.parse(data.eTime)

        console.log(d1);
        console.log(d2);
        
        if (d1 < d2) {
            wx.showToast({
              title: '暂未开放',
              icon:'error'
            })
        }
        else if (d1 > d3) {
            wx.showToast({
                title: '抢号已结束',
                icon:'error'
              })
        }
        else if (data.count == 0) {
            wx.showToast({
                title: '号已抢空!',
                icon: "error"
            })
        } else {
            console.log("执行抢号");
            var user = this.data.user
            var name = user.name
            var no = user.no
            var tel = user.tel
            var title = data.title
            var id = data.id
            var ticketNo = data.countSum - data.count + 1
            db2.add({
                data: {
                    name: name,
                    no: no,
                    ticketNo: ticketNo,
                    title: title,
                    tel:tel,
                    id:id
                },
                success: (res) => {
                    wx.showToast({
                        title: '抢号成功!',
                    })
                    console.log("抢号成功!");
                    console.log(res);
                    //抢号成功后更新数据

                    var count = data.count
                    console.log(count);
                    db.where({
                        _id: data._id
                    }).update({
                        data: {
                            count: count - 1
                        },
                        success: (res) => {
                            console.log("数据更新：");
                            console.log(res);
                            this.onLoad()
                        }
                    })
                }
            })
        }

    },

    //获取选号名单
    getResultList(e) {
        var user = this.data.user
        var no = user.no
        console.log("当前用户工号：" + no);
        db2.where({
            no: no
        }).orderBy('_id','desc').get({
            success: (res) => {
                console.log("当前用户选号数据：");
                console.log(res.data);
                this.setData({
                    resultList: res.data
                })
                //判断用户是否已选
                this.isGetTicket()
            }
        })
    },

    onPullDownRefresh() {
        wx.showToast({
            title: '刷新数据',
            icon: 'loading'
        })
        this.getResultList()
        this.getTicketList()
        wx.stopPullDownRefresh({
            success: (res) => {
                wx.showToast({
                    title: '刷新成功',
                    icon: 'success'
                })
            },
        })
    },
    //退出登录
    exit() {
        wx.navigateTo({
            url: '../login/login',
        })
        //删除缓存
        wx.removeStorageSync('user')
    },

    //提示弹窗
    Dialog() {
        wx.showModal({
            content:'退出登录',
            success:(res)=>{
                if (res.confirm) {
                    console.log("退出登录");
                    this.exit()
                }else if (res.cancel) {
                    console.log("用户取消操作");
                }
            }
        })
    },

    //判断用户是否已抢号
    isGetTicket(){
        
        console.log("判断抢号：");
        var r = this.data.resultList
        var t = this.data.ticketList
        console.log("当前用户抢号：");
        console.log(r);
        console.log("抢号列表：");
        console.log(t);

        for (let i = 0; i < t.length; i++) {
            for (let j = 0; j < r.length; j++) {
                if (t[i].id == r[j].id) {
                    t[i].checked = 1
                    console.log("已抢：");
                    console.log(t[i].title);
                }
            }
        }
        this.setData({
            ticketList:t
        })
    },
    
    onLoad() {

        this.getTicketList()
        this.setData({
            user: wx.getStorageSync('user')
        })
        var user = this.data.user
        console.log("当前日期：" + DATE);
        console.log("当前用户信息：");
        console.log(user);
    }
})